package com.atwork.wuhua.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.atwork.wuhua.R;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.TypeSelectRegionBean;
import com.atwork.wuhua.dialog.PopRegion;
import com.atwork.wuhua.mvp.presenter.PostPresenter;
import com.atwork.wuhua.mvp.view.IPostActivity;
import com.atwork.wuhua.utils.GlideUtils.GlideUtil;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.wrideg.PictureAndTextEditorView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 发帖
 */
public class PostActivity extends BaseActivity implements IPostActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_region)
    TextView tvRegion;
    @BindView(R.id.ed_title)
    EditText edTitle;
    @BindView(R.id.ed_content)
    PictureAndTextEditorView edContent;


    private PostPresenter presenter;
    private PopRegion popRegion;
    private List<TypeSelectRegionBean.DataBean> typeSelectRegionBeans;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_post;
    }

    @Override
    public void initView() {
        tvTitle.setText("发帖");
        tvRight.setVisibility(View.VISIBLE);
        presenter = new PostPresenter();
        presenter.attachView(this);
        typeSelectRegionBeans = new ArrayList<>();

    }

    @Override
    public void initData() {
        presenter.getRegion();
    }

    @OnClick({R.id.img_left, R.id.rly_region, R.id.img_add, R.id.tv_action})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.rly_region:
                popRegion = new PopRegion(this, typeSelectRegionBeans, new PopRegion.OnSelectLinstener() {
                    @Override
                    public void onselect(int positon) {
                        tvRegion.setText(typeSelectRegionBeans.get(positon).getName());
                        popRegion.dismiss();
                    }
                });
                popRegion.showAsDropDown(view);
                break;
            case R.id.img_add:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                        .maxSelectNum(1)// 最大图片选择数量 int
                        .minSelectNum(1)// 最小选择数量 int
                        .imageSpanCount(3)// 每行显示个数 int
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                        .previewImage(true)// 是否可预览图片 true or false
                        .previewVideo(false)// 是否可预览视频 true or false
                        .enablePreviewAudio(false) // 是否可播放音频 true or false
                        .isCamera(true)// 是否显示拍照按钮 true or false
                        .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                        .setOutputCameraPath("/CustomPath")// 自定义拍照保存路径,可不填
                        .enableCrop(true)// 是否裁剪 true or false
                        .compress(true)// 是否压缩 true or false
                        .withAspectRatio(1, 1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽 true or false
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
                        .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                        .showCropGrid(true)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                        .openClickSound(false)// 是否开启点击声音 true or false
                        .minimumCompressSize(100)// 小于100kb的图片不压缩
                        .synOrAsy(true)//同步true或异步false 压缩 默认同步
                        .rotateEnabled(false) // 裁剪是否可旋转图片 true or false
                        .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                        .isDragFrame(true)// 是否可拖动裁剪框(固定)
                        .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
                break;
            case R.id.tv_action:
                List<String> strings = edContent.getmContentList();
//                presenter.postImage(strings);
                presenter.postIssuance(strings);
                break;
        }
    }

    @Override
    public void getRegionResult(TypeSelectRegionBean bean) {
        typeSelectRegionBeans.clear();
        typeSelectRegionBeans.add(new TypeSelectRegionBean.DataBean("0", "不限区域"));
        typeSelectRegionBeans.addAll(bean.getData());
        typeSelectRegionBeans.get(0).setSelect(true);
    }

    @Override
    public void upLoadImgResult(String urlPath) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    for (int i = 0; i < selectList.size(); i++) {
                        if (selectList.get(i).isCut() == true) {
                            LogUtils.e("path==>" + selectList.get(i).getCutPath());
//                            presenter.uploadImg(selectList.get(i).getCutPath());
                            edContent.insertBitmap(selectList.get(i).getCutPath());
                        }
                    }

                    break;
            }
        }
    }
}
